package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CharObjidTable {
  private static final Log _log = LogFactory.getLog(CharObjidTable.class);
  
  private static final Map<Integer, String> _objList = new HashMap<>();
  
  private static final Map<Integer, String> _objClanList = new HashMap<>();
  
  private static CharObjidTable _instance;
  
  public static CharObjidTable get() {
    if (_instance == null)
      _instance = new CharObjidTable(); 
    return _instance;
  }
  
  public void load() {
    loadPc();
    loadClan();
  }
  
  private void loadClan() {
    Connection cn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement("SELECT * FROM `clan_data`");
      rs = ps.executeQuery();
      while (rs.next()) {
        int clan_id = rs.getInt("clan_id");
        String clan_name = rs.getString("clan_name");
        _objClanList.put(Integer.valueOf(clan_id), clan_name);
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
  }
  
  private void loadPc() {
    Connection cn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement("SELECT * FROM `characters`");
      rs = ps.executeQuery();
      while (rs.next()) {
        int objid = rs.getInt("objid");
        String char_name = rs.getString("char_name");
        _objList.put(Integer.valueOf(objid), char_name);
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
  }
  
  public void reChar(int objid, String char_name) {
    _objList.put(Integer.valueOf(objid), char_name);
  }
  
  public void addChar(int objid, String char_name) {
    int tmp = charObjid(char_name);
    if (tmp == 0)
      _objList.put(Integer.valueOf(objid), char_name); 
  }
  
  public String isChar(int objid) {
    String username = _objList.get(Integer.valueOf(objid));
    if (username != null)
      return username; 
    return null;
  }
  
  public int charObjid(String char_name) {
    Iterator<Integer> iterator = _objList.keySet().iterator();
    while (iterator.hasNext()) {
      Integer integer = iterator.next();
      String tgname = _objList.get(integer);
      if (char_name.equalsIgnoreCase(tgname))
        return integer.intValue(); 
    } 
    return 0;
  }
  
  public void charRemove(String char_name) {
    int objid = charObjid(char_name);
    String username = _objList.get(Integer.valueOf(objid));
    if (username != null)
      _objList.remove(Integer.valueOf(objid)); 
  }
  
  public void addClan(int clan_id, String clan_name) {
    int tmp = clanObjid(clan_name);
    if (tmp == 0)
      _objClanList.put(Integer.valueOf(clan_id), clan_name); 
  }
  
  public boolean isClan(int clan_id) {
    String username = _objClanList.get(Integer.valueOf(clan_id));
    return (username != null);
  }
  
  public int clanObjid(String clan_name) {
    Iterator<Integer> iterator = _objClanList.keySet().iterator();
    while (iterator.hasNext()) {
      Integer integer = iterator.next();
      String tgname = _objClanList.get(integer);
      if (clan_name.equalsIgnoreCase(tgname))
        return integer.intValue(); 
    } 
    return 0;
  }
}
